package com.example.demofx;


import cn.hutool.json.JSONUtil;


import com.example.demofx.data.IP.Data;
import com.example.demofx.data.IP.Ip;
import com.example.demofx.data.IPHONE.Iphone;
import com.example.demofx.data.OILprice.Oilprice;
import com.example.demofx.dbHelper.DBHelper;
import com.example.demofx.util.HttPClient;
import com.example.demofx.util.Http2;
import com.example.demofx.util.https;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;


public class HelloController {
    @FXML
    private Label OilpriceLabel;
    @FXML
    private TextField OilpriceText;
    @FXML
    private Label IphoneLabel;
    @FXML
    private TextField IphoneText;
    @FXML
    private TextField IPText;
    @FXML
    private Label IPLabel;


    @FXML
    protected void onaButtonClick() throws SQLException {
        String name = OilpriceText.getText();
        boolean flag = false;
        String dbUser = "root";
        String dbPassword = "root";
        String dbName = "test";
        String connUrl = "jdbc:mysql://localhost:3306/" + dbName + "?useUnicode=true&useSSL=false";
        Connection conn = DBHelper.getMySQLConnection(connUrl, dbUser, dbPassword);
        String sqlQuery = "select * from test1";
        ResultSet rs = DBHelper.executeQuery(conn, sqlQuery);
        while (rs.next()) {
            if (flag = rs.getString(1).equals(name))
                break;
            }if(flag){
            OilpriceLabel.setText("数据库的油价信息当前省份 :"+rs.getString(1)+"\n"+"当前0号柴油价格 : " +rs.getString(2)
            +"元"+"\n"+"当前89号汽油价格 : "+rs.getString(3)+"元"+"\n"+"当前92号汽油价格 : "+rs.getString(4)+"元"+"\n"+"当前95号汽油价格 : "
            +rs.getString(5)+"元");
            }
            else {
                String url = "https://qqlykm.cn/api/oilprice/get?key=7tPU4eeOsSORX0DBlggx3FF21q&name=" + name;
                String data = HttPClient.Get(url);
            Oilprice oilprice = JSONUtil.toBean(data, Oilprice.class);
                String msg = "当前省份 : " + name+"\n";
                msg += "当前0号柴油价格 : " + oilprice.getData().getP0()+"元"+"\n";
                msg += "当前89号汽油价格 : " + oilprice.getData().getP89()+"元"+"\n";
                msg += "当前92号汽油价格 : " + oilprice.getData().getP92()+"元"+"\n";
                msg += "当前95号汽油价格 : " + oilprice.getData().getP95()+"元"+"\n";
            OilpriceLabel.setText(msg);
                DBHelper.executeUpdate(conn,"insert into test1 values('"+name+"','" +oilprice.getData().getP0()+
                        "','"+oilprice.getData().getP89()+"','"+oilprice.getData().getP92()+"','"+oilprice.getData().getP95()+"')");
            }
        }

    @FXML
    protected void onbButtonClick()throws SQLException {
        String Iphone = IphoneText.getText();
        boolean flag = false;
        String dbUser = "root";
        String dbPassword = "root";
        String dbName = "test";
        String connUrl = "jdbc:mysql://localhost:3306/" + dbName + "?useUnicode=true&useSSL=false";
        Connection conn = DBHelper.getMySQLConnection(connUrl, dbUser, dbPassword);
        String sqlQuery = "select * from test2";
        ResultSet rs = DBHelper.executeQuery(conn, sqlQuery);
        while (rs.next()) {
            if (flag = rs.getString(1).equals(Iphone))
                break;
        }
        if (flag) {
            IphoneLabel.setText("数据库电话号码信息 :" + rs.getString(1) + "\n" +"电话所属的城市 : " + rs.getString(2) + rs.getString(3)+"\n"
            +"电话号码的地区代码 : "+rs.getString(4)+"\n"+"电话号码的邮政编码 : "+rs.getString(5)+"\n"+"电话号码的运营商 : "+rs.getString(6));
        } else {
            String url = "https://qqlykm.cn/api/free/phone/get?key=7tPU4eeOsSORX0DBlggx3FF21q&phone=" + Iphone;
            String data = https.Get(url);
            Iphone iphone = JSONUtil.toBean(data, Iphone.class);
            String msg = "输入的电话号码 : " +Iphone+ "\n";
            msg+="电话所属的城市 : "+iphone.getData().getProvince()+iphone.getData().getCity()+"\n";
            msg+="电话号码的地区代码 : "+iphone.getData().getArea_code()+"\n";
            msg+="电话号码的邮政编码 : "+iphone.getData().getPost_code()+"\n";
            msg+="电话号码的运营商 : "+iphone.getData().getIsp()+"\n";
            IphoneLabel.setText(msg);
         DBHelper.executeUpdate(conn, "insert into test2 values('" + Iphone + "','" +iphone.getData().getProvince() + "','" + iphone.getData().getCity()
                 + "','"+iphone.getData().getArea_code()+ "','"+iphone.getData().getPost_code()+"','"+iphone.getData().getIsp()+"')");

        }

    }



    @FXML
    public void oncButtonClick()throws SQLException {
        String Ip = IPText.getText();
        boolean flag = false;
        String dbUser = "root";
        String dbPassword = "root";
        String dbName = "test";
        String connUrl = "jdbc:mysql://localhost:3306/" + dbName + "?useUnicode=true&useSSL=false";
        Connection conn = DBHelper.getMySQLConnection(connUrl, dbUser, dbPassword);
        String sqlQuery = "select * from test3";
        ResultSet rs = DBHelper.executeQuery(conn, sqlQuery);
        while (rs.next()) {
            if (flag = rs.getString(1).equals(Ip))
                break;
        }
        if (flag) {
            IPLabel.setText("数据库ip信息: "+ rs.getString(1) + "\n" +"当前ip位置: "+ rs.getString(2));

        } else {
            String url = "https://qqlykm.cn/api/free/ip/get?key=7tPU4eeOsSORX0DBlggx3FF21q&ip="+Ip;
            String data = Http2.Get(url);
            Ip ip = JSONUtil.toBean(data, Ip.class);
            String msg = "当前ip: " + ip.getData().getIp()+ "\n";
            msg += "当前ip位置 " +ip.getData().getLocation() + "\n";
            IPLabel.setText(msg);
            DBHelper.executeUpdate(conn, "insert into test3 values('" + ip.getData().getIp() + "','" + ip.getData().getLocation()+ "')");
        }
    }
}